﻿using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.PhantomJS;
using OpenQA.Selenium.Support.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using Helpers.Config;

namespace Helpers
{
    public class SeleniumHelper
    {

        public static IWebDriver CreateFirefoxDriver(FirefoxConfig config)
        {
            FirefoxDriverService firefoxDriverService = null;
            //设置gecko驱动路径
            if (config.UseDriver)
            {
                firefoxDriverService = FirefoxDriverService.CreateDefaultService(config.DriverPath, config.DriverName);
            }
            else
                firefoxDriverService = FirefoxDriverService.CreateDefaultService();

            //设置浏览器路径
            if (config.UseBrowser)
                firefoxDriverService.FirefoxBinaryPath = config.BrowserPath;

            firefoxDriverService.HideCommandPromptWindow = config.HideCommandPromptWindow;

            FirefoxOptions options = new FirefoxOptions();
            //设置用户代理
            if (config.UseRandomUserAgent)
                options.SetPreference("general.useragent.override", UserAgentDefine.GetRandomUserAgent());
            else
                options.SetPreference("general.useragent.override", config.UserAgent);

            //是否显示图片
            if (!config.ShowImage)
                options.SetPreference("permissions.default.image", 2);


            //options.SetPreference("browser.cache.check_doc_frequency", 2);//从不检查缓存
            options.SetPreference("browser.startup.page", 0);//启动时显示空白
            options.SetPreference("browser.shell.checkDefaultBrowser", false);//关闭检查默认浏览器
            options.SetPreference("browser.search.update", false);//关闭更新
            //options.SetPreference("browser.cache.memory.enable", true);//缓存大小
            //options.SetPreference("browser.cache.memory.capacity", false);//缓存大小

            FirefoxProfile profile;
            //设置profile的路径
            if (config.UseProfile)
                profile = new FirefoxProfile(config.ProfilePath);
            else
            {
                profile = new FirefoxProfile();
                //options.AddArgument("--safe-mode");//安全模式
            }
            profile.DeleteAfterUse = true;
            options.Profile = profile;

            //是否无头
            if (config.UseHeadless)
            {
                options.AddArgument("--headless");
                //options.AddArgument("--disable-gpu");
            }

            var driver = new FirefoxDriver(firefoxDriverService, options, TimeSpan.FromSeconds(100));
            return driver;
        }

        public static IWebDriver CreatePhantomJSDriver(PhantomJSConfig config)
        {
            var service = PhantomJSDriverService.CreateDefaultService(config.DriverPath);
            service.HideCommandPromptWindow = config.HideCommandPromptWindow;
            service.LoadImages = config.LoadImages;
            service.IgnoreSslErrors = config.IgnoreSslErrors;
            service.SslProtocol = config.SslProtocol;
            if (config.UseProxy)
            {
                service.ProxyType = config.ProxyType;
                service.Proxy = config.Proxy;
                service.ProxyAuthentication = config.ProxyAuthentication;
            }

            PhantomJSOptions options = new PhantomJSOptions();
            options.AddAdditionalCapability("phantomjs.page.settings.userAgent", config.UserAgent);
            //options.AddAdditionalCapability("phantomjs.page.settings.Accept", "application/json, */*");
            //options.AddAdditionalCapability("phantomjs.page.settings.Accept", "*/*");
            IWebDriver driver = new PhantomJSDriver(service, options, config.CommandTimeout);
            return driver;
        }


    }
}
